const api = require('../../api/store_api');
import { toast, log, err, to } from '../../utils/wxapi';

Page({
  /**
   * 页面的初始数据
   */
  data: {
    cart_list: [],
    sum_price: 0,
    sum_count: 0,
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.cartList();
  },
  async cartList() {
    try {
      const data = JSON.stringify({
        deskId: wx.getStorageSync('deskId'),
      });
      const res = await api.cartList(data);
      log(res);
      if (res.data.respCode === 200) {
        let cart_list = JSON.parse(res.data.source);
        cart_list = this.img(cart_list);
        this.setData({ cart_list });
        this.sum(cart_list);
      }
    } catch (error) {
      err(error);
      throw error;
    }
  },
  img(arr) {
    arr.forEach(e => {
      e.goodsBO.coverUrl = JSON.parse(e.goodsBO.coverUrl);
    });
    return arr;
  },
  sum(arr) {
    try {
      let sum_price = 0;
      let sum_count = 0;
      arr.forEach(e => {
        sum_price += e.goodsBO.price * e.goodsCount;
        sum_count += e.goodsCount;
      });
      sum_price = sum_price.toFixed(2);
      this.setData({
        sum_price,
        sum_count,
      });
    } catch (error) {
      err(error);
      throw error;
    }
  },
  async postcart() {
    try {
      const data = JSON.stringify({
        deskId: wx.getStorageSync('deskId'),
      });
      const res = await api.cartList(data);
      if (res.data.respCode === 200) {
        let cart_list = JSON.parse(res.data.source);
        cart_list = this.img(cart_list);
        this.setData({ cart_list });
        this.sum(cart_list);
        wx.showModal({
          title: '提示',
          content: '你确定结算当前购物车内容吗？',
          complete: async res => {
            if (res.confirm) {
              let cartIdList = [];
              this.data.cart_list.forEach(e => {
                cartIdList.push(e.id);
              });
              let deskId = wx.getStorageSync('deskId');
              let data = JSON.stringify({
                cartIdList,
                deskId,
              });
              const res = await api.orders(data);
              if (res.data.respCode === 200) {
                let id = JSON.parse(res.data.source).id;
                to('/user/pages/order/order?id=' + id);
              }
            }
          },
        });
      }
    } catch (error) {
      err(error);
      throw error;
    }
  },
  Delcart(e) {
    try {
      const id = [e.currentTarget.dataset.id];
      const data = JSON.stringify({ id });
      wx.showModal({
        title: '提示',
        content: '您确定将该菜品从购物车中移除吗？',
        complete: async res => {
          if (res.confirm) {
            const res = await api.eletcai(data);
            log(res);
            if (res.data.respCode === 200) {
              toast('删除成功');
              this.cartList();
            } else {
              toast(res.data.msg);
            }
          }
        },
      });
    } catch (error) {
      err(error);
      throw error;
    }
  },
  deleted() {
    try {
      const { cart_list } = this.data;
      const data = JSON.stringify({
        id: cart_list.map(e => e.id),
      });
      console.log(data);
      wx.showModal({
        title: '提示',
        content: '您确定将所有菜品从购物车中移除吗？',
        complete: async res => {
          if (res.confirm) {
            const res = api.eletcai(data);
            if (res.data.respCode === 200) {
              toast('删除成功');
              this.cartList();
            } else {
              toast(res.data.msg);
            }
          }
        },
      });
    } catch (error) {
      err(error);
      throw error;
    }
  },
  async change(e) {
    try {
      const item = e.currentTarget.dataset.item;
      const data = JSON.stringify({
        id: item.id,
        goodsCount: e.detail,
      });
      const res = await api.editcart(data);
      if (res.data.respCode === 200) {
        this.cartList();
      }
    } catch (error) {
      err(error);
      throw error;
    }
  },
});
